jQuery(function($) {
	var doc = document,
		doc_ = $(doc); //将document对像缓存以保证性能
		
	doc_.on("click", ".head li a", function(event) {
		var link = $(this),
			links = $(".head li a");
			
		$.each(links, function(i, item) {
			links.eq(i).removeClass("active");
		});
		link.addClass("active");
	});
	
	//兼容安桌和IOS
	
	
	//以下两个事件为显示表单
		//ios事件处理
	doc_.on("touchstart", ".nav_list .list", function(event) {
		event.preventDefault();	//阻止冒泡到click事件
		$(".nav_list .search_info").addClass("test");
		$(".nav_list .search_info input").addClass("stretching");
	});
		
	doc_.on("click", ".nav_list .list", function(event) {
		$(".nav_list .search_info").addClass("test");
		$(".nav_list .search_info input").addClass("stretching");
	});
	
		//ios事件处理
	doc_.on("touchstart", ".nav_list .proList", function(event) {
		event.preventDefault();	//阻止冒泡到click事件
		$(".nav_list .search_info input").addClass("stretching");
	});
	
	doc_.on("click", ".nav_list .proList", function(event) {
		$(".nav_list .search_info input").addClass("stretching");
	});
	
	//当不符合表单出现条件则关闭表单
	doc_.on("touchstart", function(event) {
		
		var target = event.target;
		if (target.className !== "list" && target.className !== "proList" &&
			target.tagName !== "INPUT") {
			$(".nav_list .search_info input").removeClass("stretching");
		}
		//event.preventDefault();
	});
	
	doc_.on("click", function(event) {
		var target = event.target;
		if (target.className !== "list" && target.className !== "proList" &&
			target.tagName !== "INPUT" ) {
			$(".nav_list .search_info input").removeClass("stretching");
		}
	});
	

	
	doc_.on("click", ".proList .close", function(event) {
		//console.log();
		var elm = $(this).parent();
		elm.addClass("fadeout");
		setTimeout(function() {
			elm.remove();
		}, 500);
	});
	
	//删除class=proList的列表按钮(全部删除)
	doc_.on("touchstart", ".proList .clear", function(event) {
		var elms = $(".proList p");
		$.each(elms, function(i, item) {
			elms.eq(i).addClass("fadeout");
		});
		
		//0.5秒后移除元素
		setTimeout(function() {
			$.each(elms, function(i, item) {
				elms.eq(i).remove();
			});
		}, 500);
	});
	
	doc_.on("click", ".proList .clear", function(event) {
		var elms = $(".proList p");
		$.each(elms, function(i, item) {
			elms.eq(i).addClass("fadeout");
		});
		
		//0.5秒后移除元素
		setTimeout(function() {
			$.each(elms, function(i, item) {
				elms.eq(i).remove();
			});
		}, 500);
	});
	
	//获取class="stretching"表单值，并将其加入到class="proList"中
	doc_.on("keyup", function(event) {
		
		var elm = $(".stretching"),
			val = elm.val();
		console.log(val === undefined);
		if (event.which === 13 && val !== undefined) {
			var proList = $(".proList");
			proList.append("<p>" + val + "<em class='close'></em></p>");
			elm.val("");
            getSearchMessage(getSearchMessageByTag,obj);        //进行搜索
        }
	});

    //关闭弹出层
    //鍏煎ios
    $(doc_).on("touchstart", ".mirror", function(event) {
        event.preventDefault();
        $(".mirror").hide();
        $(".p_pos").removeClass("h_move");
        $(".bdsharebuttonbox").removeClass("move");
    });

    $(doc_).on("click", ".mirror", function() {
        $(".mirror").hide();
        $(".p_pos").removeClass("h_move");
        $(".bdsharebuttonbox").removeClass("move");
    });


	//如果删除了filter-row中的值,则将index恢得到初始状态
	doc_.on("click", ".proList .close", function(event) {
		console.log();
		if (event.target.parentNode.index === 1) {
			event.target.parentNode.index = 0;
		}
		
	});
	
	//切换filter-row的值
	function setIndex() {
		var links = doc.querySelectorAll(".row nav a"),
			len = links.length;
		
		for (var i = 0;i < len; i++) {
			links[i].index = i;
		}
	}
	setIndex();
	
	//切换tab标签
	doc_.on("click", ".row a", function(event) {
		var rows = $(".row ul"),
			rowelm = $(".filter-row"),
			links = $(".row nav a"),
			tabs = $(".filter-row ul"),
			index = event.target.index;
			
		$.each(tabs, function(i, item) {
			tabs.eq(i).addClass("hide");
			tabs.eq(i).removeClass("show");
			links.eq(i).removeClass("selected");
		});
		
		rowelm.addClass("show");
		tabs.eq(index).addClass("show");
		links.eq(index).addClass("selected");
	});

    /** 标签选择 by zeopean */
    var _index  = 0 ;           //标记为是第几个选中的元素
    var tmp     = null;         //触发元素的临时保存处
    var obj     ={              //保存标签搜索键值
        keyword:'',
        phase  :'',
        field  :'',
        dist   :''
    };
    doc_.on('click' , '.filter-row ul li a' , function(){
        tmp = $(this);
        if( tmp.attr('data') >= 0 || !tmp.attr('data') || !tmp.attr('data') == undefined) {
            _index = tmp.parent('li').index() ;
            tmp.attr('data', _index);
            var text = tmp.html();
            var proList = $(".proList");
            var beforeStr   = proList.html();
            var data_index = tmp.parents('.tab').attr('data-index');
            proList.html(beforeStr + "<p>" + text + "<em class='close' data=" + _index + " data-index="+data_index+"></em></p>");
            $('.filter-'+data_index+' a').attr('data' , -1);
            var keyword = tmp.attr('keyword');

            /** 通过 标签改变 obj对象的值 */
            if(data_index == 1) obj.phase = keyword ;
            if(data_index == 2) obj.field = keyword ;
            if(data_index == 3) obj.dist  = text ;

            getSearchMessage(getSearchMessageByTag,obj);        //进行搜索

        }
    });

    /** 关闭添加的标签 */
    doc_.on('click' , '.close' , function(){
        var elm = $(this).parent();
        var flag = $(this).attr('data');
        elm.addClass("fadeout");
        setTimeout(function() {
            elm.remove();
        }, 300);
        var num = $(this).attr('data-index');
        $('.filter-'+num+' li:eq('+flag+') a').attr('data',-1);
        $('.filter-'+num+' a').attr('data' , 0);

        if(num == 1) obj.phase = '' ;
        if(num == 2) obj.field = '' ;
        if(num == 3) obj.dist  = '' ;
    });

    /**
     * ajax 请求 获取搜索信息
     * @param url
     * @param obj
     */
    function getSearchMessage(url , obj)
    {
        $.ajax({
            type:"get",
            url:url,
            dataType:"json",
            data:{
                'keyword'   : obj.keyword,
                'phase'     : obj.phase,
                'field'     : obj.field,
                'dist'      : obj.dist
            },
            success:function(obj){
                setTimeout(function(){
                    if (obj.level == "success") {
                        $(".content ul li").html('');
                        showMore(obj.data);
                    }
                },500);
            }
        });
    }

    /** ********************************************  by zeopean *************************************************** */
    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //提交项目弹出层
    var oegId = 0 ;
    $(doc_).on("click", ".member button", function(event) {
        /** 判断是否 有项目 */
        getProject();
        //获取投资人标识
        orgId = $(this).attr('data');

    });

    //提交项目
    $('.p_pos_s button').bind('click', function () {
        var projectId   = $(".p_pos_s select").val();
        jQuery.ajax({
            type:"get",
            url :CommitProjectToInvestOrg,
            dataType:"json",
            data:{'projectId':projectId , 'orgId':orgId},
            success:function(obj){
                if(obj.level === 'success'){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }else{
                    noty({text:obj.message,type:obj.level,timeout:600});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:600});
            }
        });
        $(".p_pos").removeClass("h_move");
        $(".mirror").hide();
    });

    $(doc_).on("click", ".p_pos .close", function() {
        $(".p_pos").removeClass("h_move");
        $(".mirror").hide();
    });

    /** 查看用户是否拥有 项目 */
    function getProject(){
        jQuery.ajax({
            type:"get",
            url :getProjectByUserId,
            dataType:"json",
            data:{'action':'getProject'},
            success:function(obj){
                if(obj.level === 'success'){
                    $(".p_pos").addClass("h_move");
                    $('.p_pos_s').css('display','block');
                    $('.new').hide();
                    $('.mirror').show();
                }else{
                    //该用户没有项目 创建项目
                    $(".p_pos").addClass("h_move");
                    $(".mirror").show();
                }

            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:600});
            }
        });
    }
	
});